import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger02 {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<>();
        while (true) {
            //显示界面
            System.out.println("请输入你的操作：");
            System.out.println("1.  添加");
            System.out.println("2.  删除");
            System.out.println("3.  修改");
            System.out.println("4.  查询");
            System.out.println("5.  退出");
            System.out.println("请输入对应的数字：");

            //让用户输入
            Scanner sc = new Scanner(System.in);
            String nextLine = sc.nextLine();
            //对用户输入的数字进行选择判断

            switch (nextLine) {
                case "1":
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println("thanks");
                    System.exit(0);
            }
        }
    }

    //添加学生信息
    public static void addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入你的学号：");
        String id = sc.nextLine();
        System.out.println("请输入你的姓名：");
        String name = sc.nextLine();
        System.out.println("请输入你的性别：");
        String sex = sc.nextLine();
        System.out.println("请输入你的年龄：");
        int age = sc.nextInt();

        //创建一个新的对象
        Student s = new Student();
        s.setId(id);
        s.setSex(sex);
        s.setName(name);
        s.setAge(age);

        //将输入的信息添加到数组中
        array.add(s);
        System.out.println("添加信息成功");
    }

    //删除学生信息
    public static void deleteStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号：");
        String id = sc.nextLine();

        for (int i = 0; i < array.size(); i++) {

            Student s = array.get(i);

            if (s.getId().equals(id)) {
                array.remove(i);
                break;
            }

        }
    }

    //修改学生的信息
    public static void updateStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入你的学号：");
        String id = sc.nextLine();
        System.out.println("请输入你的姓名：");
        String name = sc.nextLine();
        System.out.println("请输入你的性别：");
        String sex = sc.nextLine();
        System.out.println("请输入你的年龄：");
        int age = sc.nextInt();

        //创建一个新的对象
        Student s = new Student();
        s.setId(id);
        s.setSex(sex);
        s.setName(name);
        s.setAge(age);

        for (int i = 0; i < array.size(); i++) {
            Student stu = array.get(i);
            if (stu.getId().equals(id)) {
                array.set(i, s);
                break;
            }
        }
        System.out.println("修改学生信息成功");
    }

    //查看学生信息
    public static void findAllStudent(ArrayList<Student> array) {

        if (array.size() == 0) {
            System.out.println("目前没有学生信息存在");
            return;
        }
        System.out.println("学号\t姓名\t性别\t年龄\t");
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getSex() + "\t" + s.getAge());

        }


    }
}
